import { SquareGroup } from "./SquareGroup";
import { IPoint, TShape } from "./types";
import { getRandom, getRandomColor } from "./utils";

export class TTShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
      ],
      centerPoint,
      color
    );
  }
}

export class LShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: -2, y: 0 },
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 0, y: 1 },
      ],
      centerPoint,
      color
    );
  }
}

export class LMirrorShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: 0, y: -2 },
        { x: 0, y: -1 },
        { x: 0, y: 0 },
        { x: 1, y: 0 },
      ],
      centerPoint,
      color
    );
  }
}

export class SShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: -1, y: 1 },
      ],
      centerPoint,
      color
    );
  }
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

export class SMirrorShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: -1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 },
      ],
      centerPoint,
      color
    );
  }
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

export class SquareShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 },
      ],
      centerPoint,
      color
    );
  }
  afterRotateShape(): TShape {
    return this._shape;
  }
}

export class LineShape extends SquareGroup {
  constructor(centerPoint: IPoint, color: string) {
    super(
      [
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 2, y: 0 },
      ],
      centerPoint,
      color
    );
  }
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

export const shapes = [
  TTShape,
  LShape,
  LMirrorShape,
  SShape,
  SMirrorShape,
  SquareShape,
  LineShape,
];

/**
 * 随机生成一个俄罗斯方块 (颜色、形状)
 * @param centerPoint
 */
export function createTeris(centerPoint: IPoint) {
  const idx = getRandom(0, shapes.length);
  const color = getRandomColor();
  return new shapes[idx](centerPoint, color);
}
